#include "LQ_Transfer_Image.h"


unsigned char FH[4] = {0xa0, 0xff, 0xff, 0xa0};
unsigned char FE[4] = {0xb0, 0xb0, 0x0a, 0x0d};

void TR_driver_init(void)
{
    // spi初始化                                                        //随便填的SCB9_SEL0_P15_3 P19_1
    LQ_SPI_InitConfig(SCB9_CLK_P15_2, SCB9_MISO_P15_0, SCB9_MOSI_P15_1, ( GPIO_SPI_SEL_t)P19_1, 125000 * 16);

    // IO2引脚初始化，用于握手信号
    LQ_GPIO_Init_Config(IO2, GPIO_IN_HIGHZ);    // IO2握手线
    LQ_GPIO_Init_Config(P14_5, GPIO_IN_HIGHZ);  // IO1配置模式
    LQ_GPIO_Init_Config(TR_CS, GPIO_OUT_PP_UP); // 软件cs下拉、

    LQ_Pin_Write(P14_5, 1);
    TR_CS_H;
}
void TR_wait_startSign(uint16_t wait_us)
{
    uint32_t time = 0;

    while (1)
    {
        if (TR_IO2 == 1)
        {
            break;
        }
        else
        {
            delay_us(50);
            if ((time += 50) > wait_us)
            {
                return;
            }
        }
    }
}
void TR_wait_endSign(uint16_t wait_us)
{
    uint32_t time = 0;

    while (1)
    {
        if (TR_IO2 == 0)
        {
            break;
        }
        else
        {
            delay_us(50);
            if ((time += 50) > wait_us)
            {
                return;
            }
        }
    }
}

void TR_Write_Image(unsigned char high, unsigned char wide, unsigned char *dat)
{

    unsigned short i;
    unsigned short temp = high * wide + 8;
    unsigned char buff_T[4000];
    unsigned short frequency = temp / 4000;
    unsigned short remainder = temp % 4000;
    unsigned char img[TR_IMG_H * TR_IMG_W + 8]; // 拷贝并组合数组
    memcpy(&img[0], FH, 4);                     // 拷贝第一个数组
    memcpy(&img[4], dat, high * wide);          // 拷贝第二个数组
    memcpy(&img[4 + high * wide], FE, 4);       // 拷贝第三个数组
    for (i = 0; i < frequency; i++)             // 把图像分为N份32字节的数据
    {
        memcpy(buff_T, &img[4000 * i], 4000);
        IR_Write_byte_4000(buff_T);
    }
    memcpy(buff_T, &img[frequency * 4000], remainder);
    IR_Wirte_byte(buff_T, remainder);
}

void IR_Write_byte_4000(unsigned char *dat) // 发送默认4000个字节
{
    unsigned char buff[32];
    //    unsigned char buff_V[32];

    TR_wait_startSign(200);
    TR_CS_L;
    for (int fre = 0; fre < 125; fre++)
    {
        memcpy(buff, &dat[32 * fre], 32);
        LQ_SPI_WriteArray(SCB9, (void *)buff, 32); // 一次发送32字节的数据，spi最大的缓冲区位32个字节。
        delay_us(53);
    }
    delay_us(50);
    TR_CS_H;
    TR_wait_endSign(200);
}
void IR_Wirte_byte(unsigned char *dat, uint16_t len) // 发送指定长度，但是需要小于4000
{
    unsigned short i;
    unsigned short fre = len / 32;
    unsigned short rem = len % 32;
    unsigned char buff[32];
    TR_wait_startSign(200);
    TR_CS_L;
    for (i = 0; i < fre; i++) // 把图像分为N份32字节的数据
    {
        memcpy(buff, &dat[32 * i], 32);
        LQ_SPI_WriteArray(SCB9, (void *)buff, 32);
        delay_us(53);
    }
    if (rem != 0) // 发送不够32个字节的数据
    {
        memset(buff, 0x00, 32);
        memcpy(buff, &dat[len - rem], rem);
        LQ_SPI_WriteArray(SCB9, (void *)buff, rem);
    }
    delay_us(50);
    TR_CS_H;
    TR_wait_endSign(200);
}

void TR_Write_Image_Pixle(unsigned char height, unsigned char width, unsigned char *Pixle)
{
    uint8_t buff_T[32];
    //    uint8_t buff_R[32];
    unsigned long i;
    unsigned int pixel_total_bits = height * width;
    unsigned int pixel_total_bytes = pixel_total_bits / 8;
    unsigned int total_bytes = pixel_total_bytes + 8; // 图像数据 + 文件头4字节 + 文件尾4字节
    unsigned int frequency = total_bytes / 32;
    unsigned char remainder = total_bytes % 32;

    uint8_t img[TR_IMG_H * TR_IMG_W / 8 + 8]; // 动态大小，取决于图片尺寸

    memset(img, 0, sizeof(img));

    // 填充头部
    memcpy(&img[0], FH, 4);

    // 压缩像素
    for (int idx = 0; idx < pixel_total_bits; idx++)
    {
        int row = idx / width;
        int col = idx % width;
        if (Pixle[row * width + col] > 0)
        {
            int byteIndex = idx / 8;
            int bitOffset = idx % 8;
            img[4 + byteIndex] |= (1 << (7 - bitOffset));
        }
    }

    // 填充尾部
    memcpy(&img[4 + pixel_total_bytes], FE, 4);

    // 发送过程
    TR_wait_startSign(500);
    TR_CS_L;

    for (i = 0; i < frequency; i++)
    {
        memcpy(buff_T, &img[32 * i], 32);
        LQ_SPI_WriteArray(SCB9, (void *)buff_T, 32);
        delay_us(53);
    }
    if (remainder != 0)
    {
        memset(buff_T, 0x00, 32);
        memcpy(buff_T, &img[total_bytes - remainder], remainder);
        LQ_SPI_WriteArray(SCB9, (void *)buff_T, remainder);
        delay_us(53);
    }

    TR_CS_H;
    TR_wait_endSign(500);
}

/*LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
@函数名称：void Test_CAMERA_TR (void)
@功能说明：无线图传测试例程
@参数说明：void
@函数返回：void
@备    注：提供了灰度图像和二值化图像两种演示
QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ*/
 void Test_CAMERA_TR(void)
{
     char txt[32];
     unsigned char image_use[60][94];  // 灰度图像
     unsigned char Pixle[60][94];  // 二值化图像
     unsigned char Threshold = 150;
     
     GPIO_LED_Init();
     LCD_Init();
     LCD_CLS(u16BLACK);
     TR_driver_init();

     /* 摄像头初始化 */
     MT9V034_Init(100);

     while (1)
     {
         if (mt9v034Flag == 1)
         {
            /* 提取部分使用的数据 */
            for(int y=0;y<60;y++)
            {
              for(int x=0;x<94;x++)
              {
                image_use[y][x] = mt9v034Image[y*2][x*2];
              }
            }
            /* 清除摄像头采集完成标志位  如果不清除，则不会再次采集数据 */
            mt9v034Flag = 0;
            
#if 0  // 显示原始图像
            LCDSPI_Road(0, 0, 60, 94, (unsigned char *)&image_use);  // 图像显示
            TR_Write_Image(60, 94, (unsigned char *)image_use);

#else  // 显示二值化图像
            /* 二值化 */
            for(int y=0;y<60;y++)
            {
              for(int x=0;x<94;x++)
              {
                if(image_use[y][x]>=Threshold)Pixle[y][x] = 255;  // 白点
                else Pixle[y][x] = 0;  // 黑点
              }
            }
            LCDSPI_Road(0, 0, 60, 94, (unsigned char *)Pixle);  // 图像显示
            TR_Write_Image_Pixle(60, 94, (unsigned char *)Pixle);

            sprintf(txt, "Threshold: %4d", Threshold);
            LCD_ShowStr_24(0,4,txt, u16RED,u16BLACK);  // 二值化阈值
#endif

            LED_Ctrl(LEDALL,RVS);        //3个LED同时闪烁
         }
     }
 }

